/*****************************************************************************************
  Project   : The Impact of Unions on Non-union Wage Setting: Threats and Bargaining
  Authors   : David A. Green, Ben M. Sand, Iain G. Snoddy, Jeanne Tschopp
  Date      : August 2025
  Output    : Table 7 of Appendix (premium vs threat)
  *****************************************************************************************/


clear
set more off
cap log clear


local 1 "All"
local 3 0



// this data becomes from right before the reshape command in the main data maker file.
use ${wd}/reg_union_${ind}_${area}_All_case_0.dta, clear

// drop public sector (no establishments)
keep if detind < 47 

// union wage "premium"
egen id = group(year2 ${area} ${ind})
gen nonunion = 1-union
tsset id nonunion


gen dwage = D.reswage * -1
gen dprem = D.prem * -1 

// change in union premium
egen id2 = group(nonunion ${area} ${ind})
tsset id2 year2

gen ddwage = D.dwage
gen ddprem = D.dprem



drop id id2



reshape wide reswage union_density sample_ic reg_wgt prem union_premium n_* pn*  E_*   RTW nonunion dwage ddwage ///
		dprem ddprem N_to_U  ,  ///
		i( year detind ${area}) j(union) 
renvars *0, postdrop(1)

// Bring in Union Proportion
	merge 1:1 ${ind} ${area} year2 using  ${wd}/keyvars/propic.dta
	drop if _merge==2
	drop _merge

// Bring in ER data
	merge m:1 ${area} year2 using  ${wd}/keyvars/emp.dta
	drop if _merge==2
	drop _merge	

// new at city-ind
	merge 1:1 ${ind} ${area} year2 using  ${wd}/keyvars/union_elections_${area}_${ind}.dta
	drop _merge


egen ic = group($area $ind) 
tsset ic (year), yearly
tsset


// union + nonunion size
gen size_ic = (sample_ic + sample_ic1)

// threat
gen threat = win_frac_ic_5

gen Lthreat = L.threat

gen threatw = threat
sum threat , d
replace  threatw = r(p99) if threat>r(p99) & !missing(threat)

// panel
drop ic
egen ic = group($area $ind) 
tsset ic (year), yearly
tsset

// Sample and changes
keep if sample_ic > 10 & sample_ic1 > 10

gen ud = 1 - n_tc 
// In changes
	tsset
		foreach var of varlist threat threatw n_tc E_*  propic ud {
			gen d`var' = D.`var'
		}	

		gen dER     = D.lnEP_c
		
		
		
// Keep Industries that observed more than once each year
bys ${ind} year2: g num=_N
keep if num>1
drop num

// Controls and Cluster
//*********************************************************
egen controls=group(year2 ${ind})
egen clusters=group(year2 ${area})


bysort ic (year): gen weight = 1/(1/sqrt(size_ic)+1/sqrt(size_ic[_n-1]))
egen sw = sum(weight), by(year2)
replace weight = weight/sw
drop sw


foreach x of num 1/4{
	g dpropic_`x' = dpropic^`x'
}


		// Table notes
local notes This table displays the results of regressing the union wage premium on union threat and the $ ict $ level.  ///
			Standard errors, in parentheses, are clustered at the $clustlbl level.   
			
local cluster 	    cluster(clusters)
local absorb        year 

reghdfe ddwage dthreat  [aw=weight], ab(`absorb') `cluster'
eststo a
estadd local yfe "Yes"
estadd local indfe "No"
estadd local set " "

reghdfe ddwage dthreat [aw=weight] if detind!=41, ab(`absorb' ) `cluster'
eststo b
estadd local yfe "Yes"
estadd local indfe "No"
estadd local set " "		

replace dthreat = dthreatw

reghdfe ddwage dthreat [aw=weight] , ab(`absorb') `cluster'
eststo c
estadd local yfe "Yes"
estadd local indfe "No"
estadd local set " "	

reghdfe ddwage dthreat [aw=weight] if year2 == 2, ab(`absorb') `cluster'
eststo d
estadd local yfe " "
estadd local indfe "No"
estadd local set " "	


reghdfe ddwage dthreat [aw=weight] if year2 == 3 , ab(`absorb') `cluster'
eststo e
estadd local yfe " "
estadd local indfe "No"
estadd local set " "	

reghdfe ddwage dthreat [aw=weight] if year2 == 4, ab(`absorb') `cluster'
eststo f
estadd local yfe " "
estadd local indfe "No"
estadd local set " "	


esttab a b c d e f, se r2 scalar(  ///
		"space2  \midrule First-Stage: " ///
		"SWF1  \ \ $ F $-Stat." "SWp1 \ \ \textit{p}-val" ///
		"OverID \ \ Over-id. \textit{p}-val")	

lab var dthreat "$ \Delta \PUN $"
		
esttab a b c d e f using "${tables}//Table_7_Appendix.tex", replace  ///
		booktabs mtitles("All" "No Hosp." "All" "1980s" "1990s" "2000s") ///
		mgroups(" "  "Winsorized"  , pattern(1 0 1 0 0 0 0 ) /// 
		prefix(\multicolumn{@span}{c}{) suffix(})  ///
		span erepeat(\cmidrule(lr){@span})) ///
		b(a2) se(a2) se r2 sfmt(3) star(* 0.10 ** 0.05 *** 0.01) label  ///
		keep( dthreat  ) nonotes  tabnote(`notes')  ///
		title("Change in Union premium and threat") ///
		order(dthreat pdthreat  ) ///
		scalar( /// 
		"yfe Year " )	

		
	
		



 
